package imp.core.basics;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;


/**
 * The Input/Output-Manager offers basis methods for reading and writing text files.
 * @author Patrick Arnold
 * @since 08.09.2010
 *
 */
public class IOManager {

	
	/**
	 * Reads the content of a file and returns it as a string field. 
	 * @param path The file path.
	 * @return The input of the file as a string field (null if an error occurred).
	 */
	public static String[] loadFile( String path) {
		
		ArrayList<String> input = new ArrayList<String>();
		String line;
		
		try {
			
			BufferedReader in = new BufferedReader( new FileReader( path));
			
			while( (line = in.readLine()) != null) {
				line = line.trim();
				input.add( line);
			}
			
			in.close();
			
			
			int l = input.size();
			String[] file = new String[l];
			
			for( int i=0; i < l; i++) {
				file[i] = input.get(i);
			}
			
			
			return file;
			
			
		} catch( Exception e) {
		
			String location = ErrorHandler.getParserLocation();
			String message = "File could not be read: <br /> &nbsp &nbsp &nbsp " + path;
			ErrorHandler.error( message, location, e);
			
			return null;
		
		}
		
	}
	
	
	
	/**
	 * Writes a file.
	 * @param output The output which is to be written in a text file.
	 * @param path The path of the file.
	 * @param append Specifies whether the file should be appended or not.
	 * @return Specifies whether writing the file has been successful or not.
	 */
	public static boolean write( String output[], String path, boolean append) {
		
		try {
			
			BufferedWriter out = new BufferedWriter( new FileWriter( path, append));
			
			for( int i=0; i < output.length; i++) {
				out.write( output[i]);
				out.newLine();
			}
			
			out.close();
			
			return true;
			
		} catch( Exception e) {
			
			String location = ErrorHandler.getParserLocation();
			String message = "File could not be written: &nbsp &nbsp &nbsp <br />" + path;
			ErrorHandler.error( message, location, e);
			
			return false;
		
		}
		
	}
	
	
	
	/**
	 * Deletes a file.
	 * @param path The path of the file.
	 * @return Specifies whether deleting the file has been successful or not. If the file
	 * does not exist, true will be returned as well (because it is then already deleted).
	 */
	public static boolean deleteFile( String path) {
		
		File file = new File( path);
		if( !file.exists()) {
			return true;
		}
		
		try {
			
			file.delete();
			return true;
			
		} catch( Exception e) {
			
			String location = ErrorHandler.getParserLocation();
			String message = "File could not be deleted: &nbsp &nbsp &nbsp <br />" + path;
			ErrorHandler.error( message, location, e);
			
			return false;
		
		}
		
	}
	
	
	
	/**
	 * Checks whether a file exists or not.
	 * @param path The path to the file.
	 * @return Specifies whether a file exists or not.
	 */
	public static boolean fileExists( String path) {
		File file = new File( path);
		return file.exists();
	}
	
}
